package user.zc.action.comp;

import lombok.SneakyThrows;
import user.zc.config.value.Key;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import static javax.swing.JOptionPane.OK_CANCEL_OPTION;

public abstract class ManagerAction<T extends Key> extends JFrame{
    private JTable table;
    private DefaultTableModel tableModel;
    private java.util.List<T> datalist;
    private Consumer<T> deleteHandler;
    private BiConsumer<T,Consumer> doEditActionHandler;

    public ManagerAction(String[] titles,Consumer<T> deleteHandler,BiConsumer<T,Consumer> doEditActionHandler){
        this.deleteHandler = deleteHandler;
        this.doEditActionHandler  = doEditActionHandler;
        this.tableModel = new DefaultTableModel();
        for (String title : titles) {
            tableModel.addColumn(title);
        }
        tableModel.addColumn("编辑");
        tableModel.addColumn("删除");


        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // 关闭窗口时只关闭此窗口，不退出程序

        // 创建表格
        table = new JTable(tableModel){
           /* @Override
            public Dimension getPreferredSize() {
                return new Dimension(600, 400);
            }*/
        };
//        table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
        // 将表格放置在滚动面板中
        JScrollPane scrollPane = new JScrollPane(table){
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(600, 400);
            }
        };;
        // 将标签添加到弹出窗口的内容面板
        getContentPane().setLayout(new FlowLayout()); // 设置弹出窗口的布局为流式布局
        getContentPane().add(scrollPane); // 添加标签到弹出窗口的内容面板

        table.getColumnModel().getColumn(titles.length).setCellRenderer(new TableCellRendererButton("编辑"));
        table.getColumnModel().getColumn(titles.length).setCellEditor(new TableCellEditorButton("编辑"));

        table.getColumnModel().getColumn(titles.length+1).setCellRenderer(new TableCellRendererButton("删除"));
        table.getColumnModel().getColumn(titles.length+1).setCellEditor(new TableCellEditorButton("删除"));

        setSize(620, 445); // 设置弹出窗口大小
        setLocationRelativeTo(null); // 将弹出窗口居中显示
    }

    protected abstract java.util.List<T> refreshRow(DefaultTableModel tableModel)throws Exception;
    @SneakyThrows
    public void refresh(){

        while(tableModel.getRowCount()>0){
            tableModel.removeRow(0);
        }
        datalist  = refreshRow(tableModel);
        ((AbstractTableModel) table.getModel()).fireTableCellUpdated(datalist.size(), 0);
//        table.repaint();
    }


    class TableCellRendererButton implements TableCellRenderer {

        private String name;
        public TableCellRendererButton(String name){
            this.name = name;
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
                                                       int row, int column) {
            JButton button = new JButton(name);
            return button;
        }

    }

    class TableCellEditorButton extends DefaultCellEditor{
        private JButton btn;
        private TableCellEditorButton _this;
        public TableCellEditorButton(String name) {
            super(new JTextField());
            _this = this;
            //设置点击一次就激活，否则默认好像是点击2次激活。
            this.setClickCountToStart(1);
            btn = new JButton(name);
            btn.addActionListener(new ActionListener() {

                @Override
                @SneakyThrows
                public void actionPerformed(ActionEvent e) {
                    T selectItem = datalist.get(table.getSelectedRow());
                    if(name.equals("删除")){
                        int status = JOptionPane.showConfirmDialog(null, "是否删除【" + selectItem.getKey() + "】", "警告", OK_CANCEL_OPTION);
                        if(status==0){
                            deleteHandler.accept(selectItem);
//                            JOptionPane.showMessageDialog(null,"删除成功");
                            //让按钮失去编辑状态，不然你修改数据后重新点击这个按钮的话，是没有选中行的，到只数组越界异常
                            _this.stopCellEditing();
                            refresh();
                        }
                    }else if(name.equals("编辑")){
                        doEditActionHandler.accept(selectItem,(value)->{
                            //让按钮失去编辑状态，不然你修改数据后重新点击这个按钮的话，是没有选中行的，到只数组越界异常
                            _this.stopCellEditing();
                            refresh();
                        });
                    }
                }
            });

        }
        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {

            return btn;
        }

    }
}
